package com.kifas.controller;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;
import javax.xml.bind.JAXBElement;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.kifas.entity.TypeAkunEntity;
import com.kifas.model.xml.PersistStatus;
import com.kifas.model.xml.TypeAkunModel;
import com.kifas.service.EntityServiceManager;

@Component
@Scope("prototype")
@Path("/TypeAkuns")
public class TypeAkunController {

	private static Logger log = Logger.getLogger(TypeAkunController.class);

	@Context
	UriInfo uriInfo;

	@Autowired
	private EntityServiceManager entityManager;

	@GET
	@Produces(MediaType.TEXT_XML)
	public List<TypeAkunModel> getTypeAkuns() {
		return transformList(entityManager.getTypeAkuns());
	}

	@GET
	@Path("{id}")
	@Produces(MediaType.TEXT_XML)
	public TypeAkunModel getTypeAkun(@PathParam("id") Long idTypeAkun) {
		return transform(entityManager.getTypeAkun(idTypeAkun));
	}

	@POST
	@Produces(MediaType.APPLICATION_XML)
	@Consumes(MediaType.APPLICATION_XML)
	public PersistStatus insert(JAXBElement<TypeAkunModel> xml) {
		TypeAkunModel model = xml.getValue();
		TypeAkunEntity typeAkun = new TypeAkunEntity(null, model.getNama(), model.getDeskripsi(), model.getKode(), model.getCreatedBy());
		try {
			entityManager.persist(typeAkun);
		} catch (Exception e) {
			log.error("can not persist type akun entity", e);
			return new PersistStatus("TypeAkunEntity", "failed", "can not persist type akun entity");
		}
		return new PersistStatus("TypeAkunEntity", "success");
	}

	@PUT
	@Produces(MediaType.APPLICATION_XML)
	@Consumes(MediaType.APPLICATION_XML)
	public PersistStatus updare(JAXBElement<TypeAkunModel> xml) {
		TypeAkunModel model = xml.getValue();
		TypeAkunEntity typeAkun = entityManager.getTypeAkun(model.getIdType());
		typeAkun.setKode(model.getKode());
		typeAkun.setDeskripsi(model.getDeskripsi());
		typeAkun.setNama(model.getNama());
		try {
			entityManager.update(typeAkun);
		} catch (Exception e) {
			log.error("can not update type akun entity", e);
			return new PersistStatus("TypeAkunEntity", "failed", "can not update type akun entity");
		}
		return new PersistStatus("TypeAkunEntity", "success");
	}

	private List<TypeAkunModel> transformList(List<TypeAkunEntity> typeAkuns) {
		List<TypeAkunModel> models = new ArrayList<TypeAkunModel>();
		for (TypeAkunEntity typeAkun : typeAkuns) {
			models.add(transform(typeAkun));
		}
		return models;
	}

	private TypeAkunModel transform(TypeAkunEntity typeAkun) {
		return new TypeAkunModel(typeAkun.getIdType(), typeAkun.getNama(), typeAkun.getDeskripsi(), typeAkun.getKode(), typeAkun.getCreatedBy());
	}

}
